// Time:  O(nlogn)
// Space: O(1)

#include <vector>

using namespace std;
class Solution {
public:
    int eraseOverlapIntervals(vector<vector<int>>& intervals) {
        if (intervals.empty()) {
            return 0;
        }
        sort(intervals.begin(), intervals.end(), [](const auto& a, const auto& b) {
            return a[1] < b[1];
        });
        int result = 0;
        int prev = intervals[0][1];
        for (int i = 1; i < intervals.size(); ++i) {
            if (intervals[i][0] < prev) {
                ++result;
            } else {
                prev = intervals[i][1];
            }
        }
        return result;
    }
};
